package com.ccdt.tv.roundprogram.util;

import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlSerializer;

import java.io.StringWriter;

/**
 * Created by hfq on 2018/10/18.
 */
public class XmlHelper {

    private static XmlHelper xmlText;

    public static synchronized XmlHelper getInstance() {
        if (xmlText == null) {
            xmlText = new XmlHelper();
        }
        return xmlText;
    }

    //GetChannels接口，请求参数封装成一个 XML 文件，并转换成String
    public String writeGetChannelXmlSerial(String client) {
        XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        // client="8230110374087890" account="DEFA02403469" customerGroup="1" startAt="1" channelAssort="7"
        try {
            serializer.setOutput(writer);
            serializer.startDocument("utf-8", true);
            serializer.startTag(null, "GetChannels");
            serializer.attribute(null, "portalId", "1");
            serializer.attribute(null, "client", client);
            serializer.attribute(null, "account", "DEFA02403469");
            serializer.attribute(null, "customerGroup", "1");
            serializer.attribute(null, "startAt", "1");
            serializer.attribute(null, "channelAssort", "7");
            serializer.endTag(null, "GetChannels");
            serializer.endDocument();
            return writer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //根据channelId切台
    public String writePlayInfoXmlSerial(String channelId) {
        //client="8230110374087890" account="DEFA02403469" channelId="10001" playType="Live" terminalType="ATV"
        //channelId传channelCode的值
        XmlSerializer s = Xml.newSerializer();
        StringWriter writer = new StringWriter();
        try {
            s.setOutput(writer);
            s.startDocument("utf-8", true);
            s.startTag(null, "VirtualChannelSelectionStart");
            s.attribute(null, "channelId", channelId);
            s.attribute(null, "client", "8230110374087890");
            s.attribute(null, "account", "DEFA02403469");
            s.attribute(null, "playType", "Live");
            s.attribute(null, "terminalType", "ATV");
            s.endTag(null, "VirtualChannelSelectionStart");
            s.endDocument();
            Log.w("tag", "writePlayInfoXmlSerial: "+s.toString() );
            return writer.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
